package com.daikuan.lizh.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.webkit.WebSettings;

import com.stx.xhb.commontitlebar.CustomTitlebar;
import com.daikuan.lizh.R;
import com.daikuan.lizh.base.ToolBarActivity;

import butterknife.Bind;


/**
 * 类描述：WebView加载网络资源
 * Created by lzy on 2016/9/22.
 */
public class WebViewActivity extends ToolBarActivity {
    @Bind(R.id.toolbar)
    CustomTitlebar toolbar;
    private TinyWebView mWebview;
    private String title;
    private String webViewUrl;

    @Override
    public void onRightClick() {

    }

    @Override
    public int getLayoutId() {
        return R.layout.web_activity;
    }

    @Override
    public void initPresenter() {

    }

    @Override
    public void initView() {
        getData();
        initViews();
        loadData();
    }

    @Override
    public void initData() {

    }

    /**
     * 方法描述：接收数据
     */
    private void getData() {
        webViewUrl = getIntent().getStringExtra("webview_url");
        title = getIntent().getStringExtra("webview_title");
    }

    /**
     * 方法描述：初始化WebView
     */
    private void initViews() {
        mWebview = (TinyWebView) findViewById(R.id.progress_webview);
        //web资源
        mWebview.loadUrl(webViewUrl);
    }

    /**
     * 方法描述：加载数据
     */
    private void loadData() {
        if (!TextUtils.isEmpty(title))
            toolbar.setTilte(title);

        if (TextUtils.isEmpty(webViewUrl))
            mWebview.loadUrl(webViewUrl);
    }

    /**
     * 方法描述：改写物理按键——返回的逻辑
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (mWebview.canGoBack()) {
                mWebview.goBack();//返回上一页面
                return true;
            } else {
                finish();
            }
        }
        return super.onKeyDown(keyCode, event);
    }

    /**
     * 方法描述：跳转至本Activity
     *
     * @param activity     发起跳转的Activity
     * @param webviewUrl   WebView的url
     * @param webviewTitle WebView页面的标题
     */
    public static void skip(Activity activity, String webviewUrl, String webviewTitle) {
        Intent intent = new Intent(activity, WebViewActivity.class);
        intent.putExtra("webview_url", webviewUrl);
        intent.putExtra("webview_title", webviewTitle);
        activity.startActivity(intent);
    }
}
